package com.sinog.biz.util;

import com.sinog.core.utilbean.Constants;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.select.SetOperationList;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 使用JsqlParse解析sql语句
 * @author wangxj
 * @date 2022/11/25 下午3:14
 */
public class SqlPaserUtil {

    /**
     * 获取sql所有的参数以map附上默认值返回
     * @param sql sql
     * @param statementId statementId
     * @return Map<String,String> resultMap
     * @throws JSQLParserException
     */
    public static Map<String, String> getColumnForSql(String sql,String statementId) throws JSQLParserException {
        if("com.sinog.biz.mapper.havexml.PrisonerMainMapper.getInmatesList".equals(statementId)
            || "com.sinog.biz.mapper.havexml.PrisonerMainExcelTempMapper.getInmatesList".equals(statementId)
            || "com.sinog.biz.mapper.havexml.PrisonerSrMapper.getSrInfo".equals(statementId)
            || "com.sinog.biz.mapper.havexml.PrisonerZnMapper.getNoAdultList".equals(statementId)
            || "com.sinog.biz.mapper.havexml.PrisonerSrMapper.getShgxParentList".equals(statementId)
            || "com.sinog.biz.mapper.havexml.SocialTrainingMapper.getSocialTrainingList".equals(statementId)
            || "com.sinog.biz.mapper.havexml.GovernmentHelpMapper.getxxblList".equals(statementId)
        ) {
            Map<String, String> resultMap = new HashMap<>(16);
            CCJSqlParserManager parserManager = new CCJSqlParserManager();
            Select select = (Select) parserManager.parse(new StringReader(sql));
            PlainSelect plain;
            if(sql.contains("UNION") || sql.contains("union")) {
                SetOperationList setOperationList = (SetOperationList)select.getSelectBody();
                List<SelectBody> selects = setOperationList.getSelects();
                plain = (PlainSelect)selects.get(0);
            } else {
                plain = (PlainSelect)select.getSelectBody();
            }
            List selectitems = plain.getSelectItems();
            for (int i = 0; i < selectitems.size(); i++) {
                String columnName;
                Expression expression = ((SelectExpressionItem) selectitems.get(i)).getExpression();
                Alias alias = ((SelectExpressionItem) selectitems.get(i)).getAlias();
                if (null != alias) {
                    columnName = alias.getName();
                } else {
                    Column col = (Column) expression;
                    columnName = col.getColumnName();
                }
                resultMap.put(columnName, Constants.StrFinalValue.WU);
            }
            return resultMap;
        }
        return null;
    }
}